/**
 * file: app.js
 * description: 程序主入口  
 * author : [" t_fate@163.com "]
 * date : 2014/3/6
 */
'use strict';

var http = require('http'),
  	path = require('path'),
	  express = require('express'),
	  doT = require('express-dot'),
	  sio = require('socket.io'),
    dbHelper = require('./includes/dbHelper'),
    loadRouter = require('./loadRouter');

var app = express();

// 使用Dot模板引擎 
app.set('view engine','html');
app.engine('html', doT.__express );
app.set('view cache', true);
app.set('views',path.join(__dirname, 'views'));

app.use(express.static(path.join(__dirname , 'static')));

var server = http.createServer(app).listen(3000,function(){
	console.log('server runing port : 3000'); 
});

//绑定socket
/*
var io = sio.listen(server);
io.sockets.on('connection',function(socket){
	socket.emit('news',{ hello : 'world' });
	socket.on('my other event', function(data){
		console.log(data);
	});
});
*/

//路由加载
loadRouter(app);

// 连接Mongo数据库
var db = new dbHelper();

/*
var mongoose = require('mongoose');
require('./models/article');
var mongoConfig = {
    db_host : '127.0.0.1',
    db_port : 27017,
    username : 'tangsj',
    password : 'tsj124589',
    db_name : 'tangsj',
    poolsize : 10
}
db.connectMongo(mongoConfig,function(err, connection){
        if(!!err){
            console.log(err);
            return false;
        }
        var Article = connection.model('Article');
        var article = new Article();
        article.title = "title";
        article.body = "body";
        article.save(function(err){
            console.log(err);
        });
});
*/


// 连接Mysql 数据库
/*
var mysqlConfig = {
    db_host : '127.0.0.1',
    db_port : 3306,
    username : 'root',
    password : '',
    db_name : 'tangsj',
}

db.connectMysql(mysqlConfig, function(err, connection){
    if(!!err){
        console.log(err);
    }
    var sql = "INSERT INTO article(aid,title,body,author,created) VALUE (?,?,?,?,?);";

    connection.query(sql,['531993d08b1fd5e81a2f1d98','title','body','',''],function(err, rows, fields){
            console.log(err);
            console.log(rows);
            console.log(fields);
    }); 
});


db.createMysqlPool(mysqlConfig,function(err, pool){
      pool.getConnection(function(err, connection){
          if(!!err){
              console.log('mysql connect error');
          };
          connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
            if (err) throw err;
            console.log('The solution is: ', rows[0].solution);
            // 释放连接
            connection.release();
          });
    });
});
*/

